package edu.ynu.se.xiecheng.achitectureclass.service;

import org.springframework.stereotype.Service;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Service
public class PropertyService {

    File configFile = new File("src/main/resources/application.properties");

    public Map<String, String> getProperties() {
        Map<String, String> properties = new HashMap<>();
        String line;

        // 读取配置文件
        try (BufferedReader reader = new BufferedReader(new FileReader(configFile))) {
            while ((line = reader.readLine()) != null) {
                String[] entry = line.split("=", 2);
                if (entry.length == 2) {
                    properties.put(entry[0].trim(), entry[1].trim());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    public void writeProperties(Map<String, String> properties) {
        try (FileWriter writer = new FileWriter(configFile, false)) {
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                writer.write(entry.getKey() + "=" + entry.getValue() + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
